package com.powernode.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.Notice;
import com.powernode.model.Result;
import com.powernode.service.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author RenBoQing
 * @date 2024年08月17日 23:07
 * @Description 公告业务管理控制层
 */
@RestController
@RequestMapping("/shop/notice")
@Api(tags = "公告业务管理接口")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    /*
     * 多条件分页查询公告的列表
     * @author RenBoQing
     * @date 2024/8/25 13:09
     * @param current
     * @param size
     * @param title
     * @param isTop
     * @param status
     * @return com.powernode.model.Result<com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.powernode.domain.Notice>>
     */
    @ApiOperation("多条件分页查询公告的列表")
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('shop:notice:list')")
    public Result<Page<Notice>> getNoticeListPage(@RequestParam(defaultValue = "1") Long current,
                                                  @RequestParam(defaultValue = "10") Long size,
                                                  @RequestParam(required = false) String title,
                                                  @RequestParam(required = false) Integer isTop,
                                                  @RequestParam(required = false) Integer status) {
        // 构建公告分页对象
        Page<Notice> page = new Page<>(current, size);
        page = noticeService.page(page, new LambdaQueryWrapper<Notice>()
                .eq(ObjectUtil.isNotNull(status), Notice::getStatus, status)
                .eq(ObjectUtil.isNotNull(isTop), Notice::getIsTop, isTop)
                .like(StringUtils.hasText(title), Notice::getTitle, title)
        );
        return Result.success(page);
    }

    /*
     * 新增公告
     * @author RenBoQing
     * @date 2024/8/25 13:09
     * @param notice
     * @return com.powernode.model.Result<java.lang.String>
     */
    @ApiOperation("新增公告")
    @PostMapping("")
    @PreAuthorize("hasAuthority('shop:notice:page')")
    public Result<String> saveNotice(@RequestBody Notice notice) {
        Boolean flag = noticeService.saveNotice(notice);
        return Result.handle(flag);
    }
    /*
     * 查看公告信息
     * @author RenBoQing
     * @date 2024/8/25 13:36
     * @param noticeId 公告的Id
     * @return com.powernode.model.Result<com.powernode.domain.Notice>
     */
    @ApiOperation("查看公告信息")
    @GetMapping("/info/{noticeId}")
    @PreAuthorize("hasAuthority('shop:notice:info')")
    public Result<Notice> getNoticeById(@PathVariable Long noticeId) {
        return Result.success(noticeService.getById(noticeId));
    }
    /*
     * 修改公告信息
     * @author RenBoQing
     * @date 2024/8/25 13:36
     * @param notice 公告信息
     * @return com.powernode.model.Result<java.lang.String>
     */
    @ApiOperation("修改公告信息")
    @PutMapping("")
    @PreAuthorize("hasAuthority('shop:notice:update')")
    public Result<String> updateNotice(@RequestBody Notice notice) {
        boolean success = noticeService.updateNotice(notice);
        return Result.handle(success);
    }
    /*
     * 删除公告
     * @author RenBoQing
     * @date 2024/8/25 13:49
     * @param noticeId
     * @return com.powernode.model.Result<java.lang.String>
     */
    @ApiOperation("删除公告")
    @DeleteMapping("/{noticeId}")
    @PreAuthorize("hasAuthority('shop:notice:delete')")
    public Result<String> removeNotice(@PathVariable Long noticeId) {
        boolean success = noticeService.removeById(noticeId);
        return Result.handle(success);
    }
}
